# !/usr/bin/env python
# -*- coding:utf-8 -*-
from pathlib import Path

import allure
import requests
from loguru import logger

from utils.file import FileUtil

proxies = {
    'http': 'http://127.0.0.1:8888',
    'https': 'http://127.0.0.1:8888',
}


class Search(object):
    _URL_PREFIX = 'https://www.baidu.com/s'

    def __init__(self, session=None, enable_proxies=False):
        self.session = session or requests.Session()
        if enable_proxies:
            self.session.proxies = proxies
            self.session.verify = False
        self._set_header()
        self.session.params = {
            'ie': 'utf-8',
        }

    @allure.step('设置头信息')
    def _set_header(self):
        headers = {
            'User-Agent': 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10.15; rv:109.0) Gecko/20100101 Firefox/109.0',
            'Cookie': FileUtil.read_from(Path(__file__).parent / "cookies.txt").strip(),
        }
        self.session.headers = headers

    @allure.step('使用普通搜索')
    def search(self,
               keyword,
               search_type=None,
               page=None,
               per_page=None):
        per_page = per_page or 10
        logger.info(f'搜索关键词：{keyword}\n'
                    f'搜索的类型：{search_type}，2是图片，3是网页\n'
                    f'显示的为第{page}页\n'
                    f'每页有{per_page}个搜索结果')
        params = {
            'rn': per_page
        }
        if keyword is not None:
            params.update(wd=keyword)
        if search_type is not None:
            params.update(cl=search_type)
        if page is not None:
            page = (page - 1) * per_page
            params.update(pn=page)
        return self.request_with(params)

    @allure.step('使用高级搜索')
    def advance_search(self,
                       contains_all_keyword=None,
                       contains_full_keyword=None,
                       contains_any_keyword=None,
                       not_contains_keyword=None,
                       keyword_in=None,
                       qualify_site=None,
                       qualify_filetype=None,
                       qualify_time=None):
        logger.info(f'包含以下全部关键词：{contains_all_keyword}\n'
                    f'包含以下完整关键词：{contains_full_keyword}\n'
                    f'包含以下任意一个关键词：{contains_any_keyword}\n'
                    f'不包含以下关键词：{not_contains_keyword}\n'
                    f'查询关键词位于：{keyword_in}，0任意位置，1标题中，2URL中\n'
                    f'限定站点：{qualify_site}\n'
                    f'限定文档格式：{qualify_filetype}\n'
                    f'限定时间：{qualify_time}')
        params = {}
        if contains_all_keyword is not None:
            params.update(q1=contains_all_keyword)
        if contains_full_keyword is not None:
            params.update(q2=contains_full_keyword)
        if contains_any_keyword is not None:
            params.update(q3=contains_any_keyword)
        if not_contains_keyword is not None:
            params.update(q4=not_contains_keyword)
        if keyword_in is not None:
            params.update(q5=keyword_in)
        if qualify_site is not None:
            params.update(q6=qualify_site)
        if qualify_filetype is not None:
            params.update(gpc=qualify_filetype)
        if qualify_time is not None:
            params.update(ft=qualify_time)
        return self.request_with(params)

    @allure.step('发送请求')
    def request_with(self, params):
        logger.info(f'请求参数为：{params}')
        result = self.session.get(self._URL_PREFIX, params=params).text
        return result
